## config
## library()

limit <- function (inpath,outpath,quantmin,quantmax) {
	
	## input data ################################################################
	## max #########################
	datpath <- inpath
	datlist_max <- lapply(1:length(datpath), function(i) {
		
	dat_list <- 	read.table(datpath[i], header = T, fill = T,
							 sep = ",", stringsAsFactors = F)
		
	
		date <- as.character(dat_list[["Datetime"]])
	
		index <- list(substr(date, 9, 10))
    
		aggregate(dat_list, by = index, FUN = max, na.rm = T)
	}
										 )
	
	
	## min #####################
	
	datlist_min <- lapply(1:length(datpath), function(i) {
		
		dat_list <- 	read.table(datpath[i], header = T, fill = T,
							 sep = ",", stringsAsFactors = F)
	
		date <- as.character(dat_list[["Datetime"]])
	
		index <- list(substr(date, 9, 10))
    
		aggregate(dat_list, by = index, FUN = min, na.rm = T)

	}
										 )
	
	

	## test ######################################################################
	## max ##
	
	datmax <- do.call("rbind", datlist_max)
	
	
	outputmax <- lapply(11:length(datmax), function(i) {

	data <- if(is.numeric(datmax[1,i])) 
		quantile(datmax[i], probs = c(quantmax), na.rm = T, names = F)	
		}
			)
	## min ##	

	datmin <- do.call("rbind", datlist_min)
	
	
	outputmin <- lapply(11:length(datmin), function(i) {

	dat <- if(is.numeric(datmin[1,i])) 
		quantile(datmin[i], probs = c(quantmin), na.rm = T, names = F)	
		}
			)
	
	## output ####################################################################
	
	## format ##
	output_tab <- data.frame(unlist(outputmin),unlist(outputmax))
	output_tab[[1]] <- ifelse(output_tab[[1]] == -Inf | output_tab[[1]] == Inf,
														NA, output_tab[[1]])
	output_tab[[2]] <- ifelse(output_tab[[2]] == -Inf | output_tab[[2]] == Inf,
														NA, output_tab[[2]])

	## parameter names ##
	list_names <- list(names(datmax))
	list_names <- c(list_names[[1]][11:length(list_names[[1]])])
	
	
	## output ##
	write.table(output_tab, outpath, col.names = F, row.names = list_names,
							sep = ",")
	


}
	

